package com.yixun.qfbao.hander;

import com.yixun.qfbao.api.service.PremisesApiService;
import com.yixun.qfbao.apidto.SearchCount4CustomerAndPremisesReqDto;
import com.yixun.qfbao.apidto.SearchCount4CustomerAndPremisesResDto;
import com.yixun.qfbao.dto.base.BaseRequestDTO;
import com.yixun.qfbao.dto.base.BaseResponseDTO;
import com.yixun.qfbao.hander.base.CommonAbstractHander;
import com.yixun.qfbao.model.PartnerInfo;
import com.yixun.qfbao.utils.LoginUtils;
import com.yixun.qfbao.vo.SearchCount4CustomerAndPremisesReqVo;
import com.yixun.qfbao.xf.model.enums.ResultEnum;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class SearchCount4CustomerAndPremisesHander extends CommonAbstractHander {
    private static Logger logger= LoggerFactory.getLogger(SearchCount4CustomerAndPremisesHander.class);

    @Autowired
    private PremisesApiService premisesService;
    @Autowired
    private LoginUtils loginUtils;

    @Override
    public BaseResponseDTO doBusiness(BaseRequestDTO reqDto) {
        SearchCount4CustomerAndPremisesReqVo req = (SearchCount4CustomerAndPremisesReqVo)reqDto;
        SearchCount4CustomerAndPremisesReqDto searchCount4CustomerAndPremisesReqDto = req.getParams();
        BaseResponseDTO<SearchCount4CustomerAndPremisesResDto> res = new BaseResponseDTO<>();
        SearchCount4CustomerAndPremisesResDto resDto = new SearchCount4CustomerAndPremisesResDto();
        PartnerInfo user = loginUtils.getUser(searchCount4CustomerAndPremisesReqDto.getUserId());
        if (user == null) {
            res.setResult(ResultEnum.USER_IS_NULL.getNo());
            res.setErrorMsg("未找到用户信息");
            return res;
        }
        searchCount4CustomerAndPremisesReqDto.setStaffId(user.getId());
        if("0".equals(user.getAccountType())){//管理员
            searchCount4CustomerAndPremisesReqDto.setStaffId(null);
            searchCount4CustomerAndPremisesReqDto.setMerchantId(user.getHigherid());
        }
        try {
            resDto = premisesService.searchCount4CustomerAndPremises(searchCount4CustomerAndPremisesReqDto);
            res.setResData(resDto);
            res.setResult(resDto.getResult());
        } catch (Exception e) {
            logger.error("SearchCount4CustomerAndPremisesHander error: {}", e);
            resDto.setResult(ResultEnum.FAIL.getNo());
            resDto.setResultMsg("获取搜索条数统计失败!");
        }
        return res;
    }
}
